$(function () {
    let $close = $("#close")
    let $number = $("#number")
    let $password = $("#password")
    let $reg = $(".reg")
    let $main = $(".main_reg")
    let haxi = location.hash.slice(4, 5)
    console.log(haxi);
    function bindingEvent() {
        $close.click(function () {
            $(".msg").hide()
        })

        $main.on("blur", "input", function () {

            if ($number.val() && $password.val()) {

                $reg.css({
                    background: "red",
                    cursor: "pointer",
                })
                return false
            }
        })

        $reg.click(function () {

            if (!($number.val() && $password.val())) {

                alert("请确认账号密码填写完整")
                return false

            }

            let options = {

                url: "http://127.0.0.1:8888/users/login",
                type: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                data: {
                    username: $number.val(),
                    password: $password.val()
                },
                dataType: "json",
                success: function (res) {
                    console.log(res);
                }
            }

            $.ajax(options)
                .done(function (res) {
                    console.log(haxi);
                    if (res.code === 1) {
                        // setTimeout(function () {
                        console.log(haxi);
                        if (!(haxi === "1")) {
                            location.href = "./index.html";
                            alert("恭喜你，登陆成功。页面将在两秒之后跳转到主页面")
                        } else {
                            location.href = "../html/car.html";
                            alert("恭喜你，登陆成功。页面将在两秒之后跳转到购物车页面")
                        }
                        // }, 2000)
                        setCookie("userid", res.user.id);
                        setCookie("token", res.token, {
                            expires: 1
                        });
                    } else {
                        alert(res.message)
                    }

                })

        })
    }
    bindingEvent()

})